package com.sky.controller.admin;

import com.sky.controller.WXPayController;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.exception.BaseException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Api("管理端订单接口")
public class AdminOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WXPayController wxPayController;


    /**
     * 订单搜索
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单分页搜索")
    public Result pageQuery(OrdersPageQueryDTO dto){
        PageResult result = orderService.pageQuery(dto);
        return Result.success(result);
    }

    /**
     * 各状态的订单数量统计
     */
    @GetMapping ("/statistics")
    @ApiOperation("各状态订单数量统计")
    public Result orderStatusCount(){
        OrderStatisticsVO orderStatisticsVO = orderService.orderStatusCount();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/details/{orderId}")
    public Result queryOrderDetail(@PathVariable Long orderId){
        OrderVO vo = orderService.queryOrderDetail(orderId);
        return Result.success(vo);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancelOrder(@RequestBody OrdersCancelDTO dto){
        try {
            orderService.cancelOrder(dto);
        } catch (Exception e) {
            throw new BaseException("该状态下无法直接取消,请联系商家");
        }
        return Result.success();
    }

    /**
     * 拒绝订单
     */
    @PutMapping("/rejection")
    @ApiOperation("拒绝订单")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO dto){
        orderService.rejectOrder(dto);
        //为用户退款
        // String orderNo = "";
        // wxPayController.refund(orderNo);
        return Result.success();
    }

}
